package top.juntower.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.juntower.pojo.Foot;
import top.juntower.pojo.FootType;
import top.juntower.pojo.dto.FootDta;
import top.juntower.pojo.vo.FootVo;
import top.juntower.pojo.vo.RankingFootVo;
import top.juntower.service.impl.EnjoyFootServiceImpl;
import top.juntower.service.impl.FootServiceImpl;
import top.juntower.utils.*;

import javax.management.relation.RoleInfo;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "食物信息")
@RestController
@RequestMapping("foot")
public class FootController {

    @Autowired
    FootServiceImpl footService;

    @Autowired
    EnjoyFootServiceImpl enjoyFootService;

    @Autowired
    private RedisUtils redisUti;

    //查询所有食物
    @ApiOperation(value = "查询所有食物",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/queryAllFoot")
    public JsonResult queryAllFoot(Integer pageNum){
        if (pageNum == null){
            return  JsonResult.error(JsonResult.NO);
        }
        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageNum,20);
        List<FootDta> feet = footService.queryAllFoot(pageNum,20);
        PageInfo<FootDta> pageInfo = new PageInfo<FootDta>(feet);
        if (feet==null){
            JsonResult.error("未查到用户");
        }
        return  JsonResult.ok(pageInfo);
    }

    //根据类别查询食物
    @ApiOperation(value = "根据类别查询食物",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ftId", value = "类型Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/queryAllFootByFtId")
    public JsonResult queryAllFootByFtId(Integer ftId,Integer pageNum){
        if (ftId == null|| pageNum == null){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,20);
        List<FootDta> footDtas = footService.queryAllFootByFtId(ftId,pageNum,20);
        PageInfo<FootDta> pageInfo = new PageInfo<FootDta>(footDtas);
        return  JsonResult.ok(pageInfo);
    }

    //查找指定食物
    @ApiOperation(value = "查找指定食物",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "fId", value = "食物Id", dataType = "Integer", required = true)
    })
    @GetMapping("/queryAllFootByFId")
    public JsonResult queryAllFootByFId(Integer userId,Integer fId){
        if (fId == null){
            return  JsonResult.error(JsonResult.NO);
        }
        if (userId == null ){
            FootVo feet = footService.queryAllFootByFId(fId);
            feet.setEfstate(false);
            return  JsonResult.ok(feet);
        }
        String s;
        String likedKey = RedisKeyUtils.getLikedKey(userId,fId);
        boolean isState = redisUti.hHasKey("isState", likedKey);
        if (isState){
            String footState = (String) redisUti.hGet("isState", likedKey);
            if (footState.equals("0")){
                s = null;
            }else {
                s = "1";
            }
        }else {
            s = enjoyFootService.queryEnjoyFootById(userId, fId);
        }

        FootVo footVo = footService.queryAllFootByFId(fId);
        if ( s == null){
            footVo.setEfstate(false);
            return  JsonResult.ok(footVo);
        }
        footVo.setEfstate(true);
        return  JsonResult.ok(footVo);
    }

    //增加访问量
    @ApiOperation(value = "增加访问量",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fId", value = "食物Id", dataType = "Integer", required = true),
    })
    @GetMapping("/updateVisits")
    public JsonResult updateVisits(Integer fId){
        if (fId == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = footService.updateVisits(fId);
        if(i>0){
            return JsonResult.ok("增加成功");
        }
        return  JsonResult.error(JsonResult.UPDATE_MISS);
    }

    //查询最高用户量数据20条
    @ApiOperation(value = "查询最高用户量数据20条",response = Foot.class)
    @GetMapping("/queryRankingFoots")
    public JsonResult queryRankingFoots(){
        List<RankingFootVo> footDtas = footService.queryRankingFoots();
        if (footDtas==null){
            return  JsonResult.error(JsonResult.NO);
        }
        return JsonResult.ok(footDtas);
    }

    //根据类别查询最高用户量数据20条
    @ApiOperation(value = "根据类别查询最高用户量数据20条",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ftId", value = "类型Id", dataType = "Integer", required = true),
    })
    @GetMapping("/queryRankingFootByFtId")
    public JsonResult queryRankingFootByFtId(Integer ftId){
        List<RankingFootVo> rankingFootVos = footService.queryRankingFootByFtId(ftId);
        if (rankingFootVos == null){
            return JsonResult.ok(JsonResult.NO);
        }
        return JsonResult.ok(rankingFootVos);
    }

    //删除食物后台管理
    @ApiOperation(value = "删除食物后台管理",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fId", value = "食物Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "fState", value = "食物状态（1 存在 0 不存在）", dataType = "Integer", required = true)
    })
    @GetMapping("/deletefootById")
    public JsonResult deletefootById(Integer fId,Integer fState){
        if (fId == null || fState == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = footService.deletefootById(fId,fState);
        if (i>0){
            return JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            return JsonResult.error(JsonResult.UPDATE_MISS);
        }
    }

    //查询所有食物后台管理
    @ApiOperation(value = "查询所有食物后台管理",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/queryAllFootBackstage")
    public JsonResult queryAllFootBackstage(Integer pageNum){
        if (pageNum == null){
            return  JsonResult.error(JsonResult.NO);
        }
        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageNum,7);
        List<FootVo> footVos = footService.queryAllFootBackstage(pageNum, 7);
        PageInfo<FootVo> pageInfo = new PageInfo<FootVo>(footVos);
        if (footVos == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //模糊查询食物后台管理
    @ApiOperation(value = "模糊查询食物后台管理",response = Foot.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "foodName", value = "食物名字", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/queryAllFootDtaLike")
    public JsonResult queryAllFootDtaLike(String foodName,Integer pageNum){
        if (pageNum == null || foodName == null){
            return  JsonResult.error(JsonResult.NO);
        }
        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageNum,7);
        List<FootDta> footDtas = footService.queryAllFootDtaLike(foodName, pageNum, 7);
        PageInfo<FootDta> pageInfo = new PageInfo<FootDta>(footDtas);
        if (footDtas == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //上传图片
    @CrossOrigin
    @ApiOperation(value = "上传图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传图片", dataType = "MultipartFile", required = true),
    })
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return JsonResult.error("请选择文件");
        }
        try {
            QiniuUtil qiniuUtil = new QiniuUtil();
            FileInputStream fileInputStream = (FileInputStream) file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String fileExtend = originalFilename.substring(originalFilename.lastIndexOf("."));
            //默认不指定key的状况下，以文件内容的hash值做为文件名
            String fileKey = UUID.randomUUID().toString().replace("-", "") + fileExtend;

            String tupain="http://juntower.top/"+fileKey;
            qiniuUtil.upload(fileInputStream,fileKey);
            return JsonResult.ok(tupain);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("上传失败");
        }
    }

    //修改食物信息后台管理
    @CrossOrigin
    @ApiOperation(value = "修改食物信息后台管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fImg", value = "食物图片", dataType = "String", required = true),
            @ApiImplicitParam(name = "fId", value = "食物Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "ftId", value = "类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "fName", value = "食物名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "fContent", value = "食物内容", dataType = "String", required = true),
            @ApiImplicitParam(name = "fMake", value = "食物制作", dataType = "String", required = true),
    })
    @PostMapping("/updateFootById")
    public JsonResult updateFootById(Integer fId,Integer ftId,
                             String fName,String fContent,String fMake,String fImg) {
            if (fId == null || ftId == null || fName == null || fContent == null || fMake == null){
                return JsonResult.error(JsonResult.NO);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("fId",fId);
            map.put("ftId",ftId);
            map.put("fName",fName);
            map.put("fContent",fContent);
            map.put("fMake",fMake);
            map.put("fImg",fImg);
        int i = footService.updateFootById(map);
        if (i>0){
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }
    }
    //新增食物后台管理
    @CrossOrigin
    @ApiOperation(value = "新增食物后台管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fImg", value = "食物图片", dataType = "String", required = true),
            @ApiImplicitParam(name = "ftId", value = "类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "fName", value = "食物名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "fContent", value = "食物内容", dataType = "String", required = true),
            @ApiImplicitParam(name = "fMake", value = "食物制作", dataType = "String", required = true),
    })
    @PostMapping("/addFoot")
    public JsonResult addFoot(Integer ftId,
                             String fName,String fContent,String fMake,String fImg) {
        if (ftId == null || fName == null || fContent == null || fMake == null){
            return JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ftId",ftId);
        map.put("fName",fName);
        map.put("fContent",fContent);
        map.put("fMake",fMake);
        map.put("fImg",fImg);
        int i = footService.addFoot(map);
        if (i>0){
            return  JsonResult.ok(JsonResult.INSERT_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.INSERT_MISS);
        }
    }

    //查询所有食物类别
    @ApiOperation(value = "查询所有食物类别",response = Foot.class)
    @GetMapping("/queryFootType")
    public JsonResult queryFootType(){
        //使用分页插件,核心代码就这一行
        List<FootType> footTypes = footService.queryFootType();
        if (footTypes == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return JsonResult.ok(footTypes);
        }

    }
}
